package com.crm.system.controller.followPlanManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.followPlanManager.CpFollowPlan;
import com.crm.model.qo.followPlanManager.CpFollowPlanQO;
import com.crm.model.vo.followPlanManager.CpFollowPlanVO;
import com.crm.model.vo.followPlanManager.FollowPlanContactListVO;
import com.crm.model.vo.followPlanManager.FollowPlanCountVO;
import com.crm.system.service.followPlanManager.FollowPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: chaifuyou
 * @date 2021/3/31 15:28
 */
@RestController
@RequestMapping("/followPlan")
@Slf4j
public class FollowPlanController {
    @Autowired
    private FollowPlanService followPlanService;

    /**
     * 跟进计划列表
     * @return
     */
    @PostMapping("/followPlanList")
    public DataResponse selectFollowPlanList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                  @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                  @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                  @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                  CpFollowPlanQO cpFollowPlanQO//查询条件
    ){
        try{
            DhPageInfo<CpFollowPlanVO> page = followPlanService.selectFollowPlanList(cpFollowPlanQO,pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),page);
        }catch(Exception e){
            log.error("followPlan followPlanList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 新增跟进计划
     * @param cpFollowPlan
     * @return
     */
    @PostMapping("/addFollowPlan")
    public DataResponse addFollowPlan(@RequestBody CpFollowPlan cpFollowPlan){
        try{
            followPlanService.addFollowPlan(cpFollowPlan);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            log.error("followPlan addFollowPlan error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 修改跟进计划
     * @param cpFollowPlan
     * @return
     */
    @PostMapping("/updateFollowPlan")
    public DataResponse updateFollowPlan(@RequestBody CpFollowPlan cpFollowPlan){
        try{
            followPlanService.updateFollowPlan(cpFollowPlan);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            log.error("followPlan addFollowPlan error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 取消跟进计划
     * @param cpFollowPlanId
     * @return
     */
    @PostMapping("/deleteFollowPlan")
    public DataResponse deleteFollowPlan(@RequestParam String cpFollowPlanId,Integer type){
        try{
            boolean result = followPlanService.deleteFollowPlan(cpFollowPlanId,type);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else{
                return new DhResponse(ResponseCodeEnum.BUSINESS_ERROR.getResponseCode());//业务错误
            }
        }catch(Exception e){
            log.error("followPlan deleteFollowPlan error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 统计日期的跟进情况
     * @param planDate
     * @return
     */
    @GetMapping("/getFollowPlanCount")
    public DataResponse getFollowPlanCount(@RequestParam(value="planDate",required = false) String planDate,
                                           @RequestParam(value="startDate",required = false) String startDate,
                                           @RequestParam(value="endDate",required = false) String endDate){
        try{
            FollowPlanCountVO vo= followPlanService.getFollowPlanCount(planDate,startDate,endDate);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            log.error("followPlan getFollowPlanCount error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 当天跟进的客户列表
     * @param planDate
     * @return
     */
    @PostMapping("/getFollowPlanList")
    public DataResponse getFollowPlanListByDate(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                @RequestParam(value="planDate",required = false) String planDate,
                                                @RequestParam(value="dateType",required = false) String dateType,
                                                @RequestParam(value="customerId",required = false) String customerId){
        try{
            DhPageInfo<FollowPlanContactListVO> vo= followPlanService.getFollowPlanListByDate(planDate,pageIndex,pageSize,sortName,sortOrder,dateType,customerId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            log.error("followPlan getFollowPlanList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

}
